package com.xd.clap.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xd.clap.entity.Admin;
import com.xd.clap.entity.FirstAddress;
import com.xd.clap.entity.SecondAddress;
import com.xd.clap.entity.request.AddressParam;
import com.xd.clap.entity.response.AddressListResult;
import com.xd.clap.entity.response.Result;
import com.xd.clap.mapper.AdminMapper;
import com.xd.clap.mapper.FirstAddressMapper;
import com.xd.clap.services.FirstAddressService;
import com.xd.clap.services.SecondAddressService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class FirstAddressServiceImpl implements FirstAddressService {
    @Resource
    private FirstAddressMapper fam;

    @Resource
    private FirstAddressService fas;

    @Resource
    private SecondAddressService sas;

    @Resource
    private AdminMapper am;
    @Override
    public Result getFirstAddressList() {
        return Result.success(fas.selectFirstAddressList(""), "查询成功");
    }

    @Override
    public List<FirstAddress> selectFirstAddressList(String msg) {
        LambdaQueryWrapper<FirstAddress> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(FirstAddress::getAddress,msg);
        return fam.selectList(queryWrapper);
    }

    @Override
    public Result getAllAddressList() {
        return Result.success(fas.selectAddressList(), "查询成功");
    }

    @Override
    public List<SecondAddress> selectAddressList() {
        List<FirstAddress> firstAddressList = fas.selectFirstAddressList("");
        List<SecondAddress> addressList = new ArrayList<>();
        for (FirstAddress firstAddress : firstAddressList) {
            int fid = firstAddress.getId();
            List<SecondAddress> secondAddressList = sas.selectSecondAddressList(fid);
            for (SecondAddress secondAddress : secondAddressList) {
                secondAddress.setAddress(firstAddress.getAddress() + "/" + secondAddress.getAddress());
                addressList.add(secondAddress);
            }
        }
        return addressList;
    }

    @Override
    public Result searchAddressList(String address) {
        List<SecondAddress> addressList = fas.selectAddressList();
        if(!StringUtils.isBlank(address)){
            List<SecondAddress> searchAddressList = new ArrayList<>();
            for (SecondAddress item : addressList) {
                if (item.getAddress().contains(address)) {
                    String newAddress = item.getAddress().replaceAll(address, "<a style='color:#F56C6C'>" + address + "</a>");
                    item.setAddress(newAddress);
                    searchAddressList.add(item);
                }
            }
            return Result.success(searchAddressList, "查询成功");
        }
        return Result.success(addressList, "查询成功");
    }

    @Override
    public Result updateFirstAddress(FirstAddress firstAddress) {
        if (firstAddress.getId() != null) {
            int update = fam.updateById(firstAddress);
            if (update > 0) {
                return Result.success(update, "修改成功");
            }
            return Result.fail(100, "修改失败");

        } else {
            int insert = fam.insert(firstAddress);
            if (insert > 0) {
                return Result.success(insert, "添加成功");
            }
            return Result.fail(100, "添加失败");
        }
    }

    @Override
    public Result delFirstAddress(Integer id) {
        int isDelete = fam.deleteById(id);
        if (isDelete > 0) {
            return Result.success(isDelete, "删除成功");
        }
        return Result.fail(100, "删除失败");
    }

    @Override
    public Result getAddressList() {
        List<FirstAddress> firstAddressList = fas.selectFirstAddressList("");
        List<AddressListResult > addressList = new ArrayList<>();
        for (FirstAddress firstAddress : firstAddressList) {
            AddressListResult   address = new AddressListResult();
            int fid = firstAddress.getId();
            List<SecondAddress> secondAddressList = sas.selectSecondAddressList(fid);
            address.setId(fid);
            address.setAddress(firstAddress.getAddress());
            address.setSecondAddressList(secondAddressList);
            addressList.add(address);
        }
        return Result.success(addressList,"查询成功");

    }


}
